package base

import (
	"fmt"
	"path"
	"wf-deploy-agent/cmd"
	"wf-deploy-agent/conf"
	"wf-deploy-agent/infra"
	"wf-deploy-agent/registry"
)

// IssueCommand 文件分发命令
type IssueCommand struct {
	*cmd.Command
	OrderId    int64
	TargetPath string
	SpeedLimit int
	Items      []*IssueItem
}

func (command *IssueCommand) GetOutputDir(item *IssueItem) string {
	return path.Join(conf.GetFileDir(), "issue", fmt.Sprint(command.ProjectId), fmt.Sprint(command.ModuleId), fmt.Sprint(command.OrderId), item.Version)
}

type IssueItem struct {
	ItemId    int64
	Version   string
	FileName  string
	OriginUrl string
	MD5       string
}

func (item *IssueItem) GetFilePath(command *IssueCommand) string {
	return path.Join(conf.GetFileDir(), "issue", fmt.Sprint(command.ProjectId), fmt.Sprint(command.ModuleId), fmt.Sprint(command.OrderId), item.Version, item.FileName)
}

// GetOriginUrl 获取下载源路径
func (command *IssueCommand) GetOriginUrl(item *IssueItem) string {
	originUrl := item.OriginUrl

	//获取下载源
	instance := registry.GetRepository().GetInstance(command.BatchNO)
	if instance != nil {
		originUrl = fmt.Sprintf("%s/file/issue/%d/%d/%d/%s/%s", instance.GetHttpPrefix(), command.ProjectId, command.ModuleId, command.OrderId, item.Version, item.FileName)
		infra.Logger.Infof("从注册中心拿到源地址【%s】", originUrl)
	}

	return originUrl
}
